package com.nuanshui.heatedloan.extension.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class TestInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String origin = request.getHeader("Referer");
		String requestMethod = request.getMethod();
		String siteUrl="http://10.0.0.72:8787";
		if(origin == null || requestMethod == null) return true;
		if(origin.startsWith(siteUrl) && (requestMethod.equals("POST") || requestMethod.equals("GET"))) {
			HttpServletResponse responseHeaders = response;
			responseHeaders.setHeader("Access-Control-Allow-Origin", siteUrl);
			responseHeaders.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
			responseHeaders.setHeader("Access-Control-Allow-Credentials", "true");
			responseHeaders.setHeader("Access-Control-Allow-Headers", "openid,safetoken,userid,accept,channel");
			//responseHeaders.setHeader("Access-Control-Max-Age", "3600");
			System.out.println("-----------------拦截跨域POST请求---------------");
		}
		
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		//Map<String, Object> res = modelAndView.getModel();
		//System.out.println(JSON.toJSONString(res));
		//System.out.println(response.getHeaderNames().toString());
		
		//System.out.println("-----------------拦截跨域POST请求---------------");
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		//System.out.println("xxxxxxxxxxxxxxxxxxxxx------拦截方法----------------");
	}

	
}
